function varargout = avg_qual_params(varargin)

% Allow setting of "quality" params applied at averaging
% hdr.qual.mrdc_thr - new (more stringent) threshold for block-mean DC
% hdr.qual.mrac_thr - " AC
%
% hdr.qual.dc_range - DC must not vary by more than this within each epoch
% hdr.qual.ac_range - AC ??
% hdr.qual.delay_range - Delay (ps) must not vary by more than this within each epoch
% hdr.qual.mod_thr - modulation (0 to 1) must not fall below this in each epoch

% AVG_QUAL_PARAMS M-file for avg_qual_params.fig
%      AVG_QUAL_PARAMS, by itself, creates a new AVG_QUAL_PARAMS or raises the existing
%      singleton*.
%
%      H = AVG_QUAL_PARAMS returns the handle to a new AVG_QUAL_PARAMS or the handle to
%      the existing singleton*.
%
%      AVG_QUAL_PARAMS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in AVG_QUAL_PARAMS.M with the given input arguments.
%
%      AVG_QUAL_PARAMS('Property','Value',...) creates a new AVG_QUAL_PARAMS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before avg_qual_params_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to avg_qual_params_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help avg_qual_params

% Last Modified by GUIDE v2.5 07-Dec-2011 10:00:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @avg_qual_params_OpeningFcn, ...
                   'gui_OutputFcn',  @avg_qual_params_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before avg_qual_params is made visible.
function avg_qual_params_OpeningFcn(hObject, eventdata, handles, varargin)
global hdr
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to avg_qual_params (see VARARGIN)

% Choose default command line output for avg_qual_params
handles.output = hObject;

% initialze parameter values 
% (from hdr if they exist there, else rom menu defaults)
%
if isfield(hdr.qual,'mrdc_thr') % if hdr.qual.mrdc_thr exists, use it
    disp('hdr.qual.mrdc_thr exists')
    set(handles.mrdc_thr,'String',num2str(hdr.qual.mrdc_thr));
else % if hdr.mrac does not exist, make it = menu default
    disp('Setting hdr.qual.mrdc_thr')
    hdr.qual.mrdc_thr=str2num(get(handles.mrdc_thr,'String'));
end

if isfield(hdr.qual,'mrac_thr') % if hdr.qual.mrac_thr exists, use it
    disp('hdr.qual.mrac_thr exists')
    set(handles.mrac_thr,'String',num2str(hdr.qual.mrac_thr));
else % if hdr.qual.mrac does not exist, make it = menu default
    hdr.qual.mrac_thr=str2num(get(handles.mrac_thr,'String'));
    fprintf('Setting hdr.qual.mrac_thr = %5.3f\n',hdr.qual.mrac_thr)
end

if isfield(hdr.qual,'dc_range') % if hdr.qual_dc_range exists, use it
    set(handles.dc_range,'String',num2str(hdr.qual.dc_range));
else % if hdr.qual.dc_range does not exist, make it = menu default
    hdr.qual.dc_range=str2num(get(handles.dc_range,'String'));
end

if isfield(hdr.qual,'ac_min') % if hdr.qual_ac_min exists, use it
    disp('hdr.qual.ac_min exists')
    set(handles.ac_min,'String',num2str(hdr.qual.ac_min));
else % if hdr.qual.ac_min does not exist, make it = menu default
    hdr.qual.ac_min=str2num(get(handles.ac_min,'String'));
    fprintf('Setting hdr.qual.ac_min = %5.3f\n',hdr.qual.ac_min)
end

if isfield(hdr.qual,'delay_range') % if hdr.qual_delay_range exists, use it
    set(handles.qual_delay_range,'String',num2str(hdr.qual.delay_range));
else % if hdr.qual_delay_range does not exist, make it = menu default
    hdr.qual.delay_range=str2num(get(handles.qual_delay_range,'String'));
end

if isfield(hdr.qual,'mod_thr') % if hdr.mod_thr exists, use it
    set(handles.mod_thr,'String',num2str(hdr.qual.mod_thr));
else % if hdr.qual_delay_range does not exist, make it = menu default
    hdr.qual.mod_thr=str2num(get(handles.mod_thr,'String'));
end

if isfield(hdr,'n_threshold') % if hdr.n_threshold exists, use it
    set(handles.n_threshold,'String',num2str(hdr.n_threshold));
else % if hdr.qual_delay_range does not exist, make it = menu default
    hdr.n_threshold=str2num(get(handles.n_threshold,'String'));
end

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes avg_qual_params wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = avg_qual_params_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function mrdc_thr_Callback(hObject, eventdata, handles)
% hObject    handle to mrdc_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global hdr
hdr.qual.mrdc_thr= str2double(get(hObject,'String'));

function mrac_thr_Callback(hObject, eventdata, handles)
% hObject    handle to mrac_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global hdr
hdr.qual.mrac_thr= str2double(get(hObject,'String'));
fprintf('mrac_thr_Callback; Setting hdr.qual.mrac_thr = %5.3f\n',hdr.qual.mrac_thr)

% --- Executes during object creation, after setting all properties.
function mrac_thr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mrac_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function qual_delay_range_Callback(hObject, eventdata, handles)
% hObject    handle to qual_delay_range (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global hdr
% Value=get(h,'String');
% set(handles.n_points_str,'String',Value);
% guidata(h,handles);
% hdr.n_points=str2num(Value);
global hdr
disp('Executing qual_delay_range_Callback')
fred=get(hObject,'String')
hdr.qual.delay_range= str2double(get(hObject,'String'))

% --- Executes during object creation, after setting all properties.
function qual_delay_range_CreateFcn(hObject, eventdata, handles)
% hObject    handle to qual_delay_range (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in Done.
function Done_Callback(hObject, eventdata, handles)
% hObject    handle to Done (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close

function mod_thr_Callback(hObject, eventdata, handles)
% hObject    handle to mod_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of mod_thr as text
%        str2double(get(hObject,'String')) returns contents of mod_thr as a double
global hdr
hdr.qual.mod_thr= str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function mod_thr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mod_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function dc_range_Callback(hObject, eventdata, handles)
% hObject    handle to dc_range (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dc_range as text
%        str2double(get(hObject,'String')) returns contents of dc_range as a double
global hdr
hdr.qual.dc_range= str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function dc_range_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dc_range (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function ac_min_Callback(hObject, eventdata, handles)
% hObject    handle to ac_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ac_min as text
%        str2double(get(hObject,'String')) returns contents of ac_min as a double
global hdr
hdr.qual.ac_min= str2double(get(hObject,'String'));
fprintf('ac_min_Callback; hdr.qual.ac_min = %5.3f\n',hdr.qual.ac_min); % works

% --- Executes during object creation, after setting all properties.
function ac_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ac_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object deletion, before destroying properties.
function mrdc_thr_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to mrdc_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object deletion, before destroying properties.
function ac_min_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to ac_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function mrdc_thr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mrdc_thr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object deletion, before destroying properties.
function dc_range_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to dc_range (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes on key press with focus on ac_min and none of its controls.
function ac_min_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to ac_min (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over ac_min.
function ac_min_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to ac_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

function n_threshold_Callback(hObject, eventdata, handles)
% hObject    handle to n_threshold (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global hdr
hdr.n_threshold= str2double(get(hObject,'String'));
fprintf('n_threshold_Callback; hdr.n_threshold = %5.3f\n',hdr.n_threshold); % works
% Hints: get(hObject,'String') returns contents of n_threshold as text
%        str2double(get(hObject,'String')) returns contents of n_threshold as a double

% --- Executes during object creation, after setting all properties.
function n_threshold_CreateFcn(hObject, eventdata, handles)
% hObject    handle to n_threshold (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
